<?php
/**
 * 《排班管理》实现类
 *
 * @author 孙晓晔
 * @version $Id: Country.class.php 832 2010-01-06 09:17:14Z xuy $
 */
require_once(dirname(__FILE__) . '/KqUtil.class.php');

class Plan extends KqUtil {
	public $tbl = Constant::tbl_kq_plan;
	
	public $status = array(
		'-' => '请选择',
		'1' => '等待审核',
		'2' => '等待排班',
		'3' => '正在排班',
		'4' => '已经排班',

		'9' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',
		'emp' => '员工',	
		't1' => '上班时间',

		'1' => '创建时间',
		'2' => '更新时间',
	);

	public $groupby = array(
		'-' => '请选择',
		'dept' => '部门',
		'emp' => '员工',
		'status' => '状态',
		'2' => '日期',
		'3' => '月份',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		// $controller->assign('t1', strtotime('-1 month'));
		
		$emp = array(
			// '-' => '请选择',
			'emp' => '员工',
			'oper' => '录入员',
		);
		
		$controller->assign_by_ref('emp_option', $emp);

		$controller->display($request, 'kq/plan/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {
		$date1_s = $request['date1_s'];
		$date2_s = $request['date2_s'];
		
		$state_s = $request['state_s'];

		$my = $this->_my();

		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}		

		if (!empty($date1_s)) {
			$where .= " AND t1 >= '" . $date1_s . "'";
		}

		if (!empty($date2_s)) {
			$where .= " AND t2 < '" . $date2_s . "'";
		}
		
		if (!empty($state_s) && $state_s <> '-') {
			$where .= " AND state = '" . $state_s . "'";
		}
		
		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();		
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);
				
				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$row['is_delete'] = $this->_is_delete($row);

				$data[$count] = $row;
				$count++;
			}

			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'kq/plan/list');
		return true;
	}

	function _is_delete($row) {
		if ($this->_is_admin()) {
			return true;
		}

		if ($this->_audit('111')) {
			return true;
		}

		if ($row['oper'] == $_SESSION['id']) {
			return true;
		}

		return false;
	}

	function xls($controller, $request) {
		$this->pretreat($controller);
		
		$this->findPara($controller, $request);

		$request['tpl'] = "xls";

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();		
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);
		
		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);

			$row = $this->_orderby_x($controller, $row, $this->orderby_s);

			$data[] = $row;
		}

		$controller->assign_by_ref('result', $data);
		
		$filename = sprintf("%s.xls", date("YmdHis"));
		header('Content-Disposition:application/vnd.ms-excel; filename=' . $filename);

		$controller->display($request, 'kq/plan/list');
		return true;
	}

	function emp($controller, $request) {
		$this->pretreat($controller);

		$emp_s = $request['id'];

		$date1_s = $request['date1_s'];
		$date2_s = $request['date2_s'];

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE emp = '%s' AND t1 >= '%s' AND t2 <= '%s' ORDER BY t1";
		$sql = sprintf($fmt, $this->tbl, $emp_s, $date1_s, $date2_s);		debug($sql);
		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);

			$data[] = $row;
		}

		$controller->assign_by_ref('result', $data);

		$controller->display($request, 'kq/plan/emp');
		return true;
	}

	function _status($data) {
		if (strtotime($data['t2']) > time()) {
			return $this->status[$data['status']];
		}

		if ($data['status'] > 2) {
			return $this->status[$data['status']];
		}

		if ($this->_vacation($data)) {
			$status = 9;
		} else {
			$status = $this->_record($data);
		}

		return $this->status[$status];
	}

	/*
	 * 请假信息判断
	 */
	function _vacation($row) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE emp = '%s' AND t1 <= '%s' AND t2 >= '%s'";
		$sql = sprintf($fmt, Constant::tbl_kq_vacation, $row['emp'], $row['t1'], $row['t2']);
		// debug($sql);
		$result = $db->query($sql);
		if ($db->num_rows($result) > 0) {
			return true;
		}

		return false;
	}

	/*
	 * 打卡信息判断
	 */
	function _record($data) {
		$a = 0; // 上班未打卡
		$b = 0; // 下班未打卡
		$duration = 30;

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE status = '1' AND emp = '%s' AND time >= DATE_ADD('%s', INTERVAL -%s minute) AND time <= '%s'";
		$sql = sprintf($fmt, Constant::tbl_kq_record, $data['emp'], $data['t1'], $duration, $data['t1']);
		// debug($sql);
		$result = $db->query($sql);
		if ($db->num_rows($result) > 0) {
			$a = 1; // 上班正常
		}

		$fmt = "SELECT * FROM %s WHERE status = '1' AND emp = '%s' AND time > '%s' AND time <= DATE_ADD('%s', INTERVAL %s minute) ";
		$sql = sprintf($fmt, Constant::tbl_kq_record, $data['emp'], $data['t1'], $data['t1'], $duration);
		// debug($sql);
		$result = $db->query($sql);
		if ($db->num_rows($result) > 0) {
			$a = 2; // 上班迟到
		}

		$fmt = "SELECT * FROM %s WHERE status = '1' AND emp = '%s' AND time >= '%s' AND time <= DATE_ADD('%s', INTERVAL %s minute) ";
		$sql = sprintf($fmt, Constant::tbl_kq_record, $data['emp'], $data['t2'], $data['t2'], $duration);
		// debug($sql);
		$result = $db->query($sql);
		if ($db->num_rows($result) > 0) {
			$b = 1; // 下班正常
		}

		$fmt = "SELECT * FROM %s WHERE status = '1' AND emp = '%s' AND time >= DATE_ADD('%s', INTERVAL -%s minute) AND time < '%s'";
		$sql = sprintf($fmt, Constant::tbl_kq_record, $data['emp'], $data['t2'], $duration, $data['t2']);
		// debug($sql);
		$result = $db->query($sql);
		if ($db->num_rows($result) > 0) {
			$b = 2; // 下班早退
		}

		if ($a == 0) {
			return $this->_ab_a0($b);
		}

		if ($a == 1) {
			return $this->_ab_a1($b);
		}

		if ($a == 2) {
			return $this->_ab_a2($b);
		}

		return '0';
	}

	function _ab_a0($b) {
		if ($b == 1) {
			return '6';
		}

		// 上班未打卡并早退按旷工处理
		if ($b == 2) {
			return '8';
		}

		if ($b == 0) {
			return '8';
		}

		return 0;
	}

	function _ab_a1($b) {
		if ($b == 1) {
			return '3';
		}

		if ($b == 2) {
			return '5';
		}

		if ($b == 0) {
			return '7';
		}

		return 0;
	}

	function _ab_a2($b) {
		if ($b == 1) {
			return '4';
		}

		// 迟到并早退按旷工处理
		if ($b == 2) {
			return '8';
		}

		// 迟到并下班未打卡按旷工处理
		if ($b == 0) {
			return '8';
		}

		return 0;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'kq/plan/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);

		$t1 = $request['t1'];
		$t2 = $request['t2'];

		$content = addslashes($request['content']);

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(office, dept, emp, t1, t2, content, oper, status, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '2', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $t1, $t2, $content, $_SESSION['id']);
		// debug($sql);
		$db->query($sql);

		$request['id_s'] = $db->insert_id();
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function batch($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'kq/plan/batch');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function batchPost($controller, $request) {
		$this->pretreat($controller);

		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);

		$date1 = $request['date1'];
		$date2 = $request['date2'];

		$time1 = $request['time1'];
		$time2 = $request['time2'];

		$holiday = $request['holiday'];

		$content = addslashes($request['content']);

		$week = json_decode($request['week']);
		if (!$week || $week == NULL) {
			debug("week is NULL");
			return false;
		}

		$db = newdb($this->dsn);
		$fmt = "INSERT INTO %s(emp, t1, t2, content, oper, status, createtime, modifytime) ";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '2', now(), now())";

		$t1 = strtotime($date1);
		$t2 = strtotime($date2);
		for ($i = 0; $t1 < $t2; $i++) {
			$dd = date('Y-m-d', $t1);
			$ww = date('w', $t1);
			$t1 = strtotime("1 day", $t1);

			if (!in_array($ww, $week)) {
				debug($ww);
				continue;
			}

			$from = $dd . " " . $time1;
			$to = $dd . " " . $time2;
			if ($this->_holiday($holiday, $from, $to)) {
				debug("holiday...");
				continue;
			}

			$sql = sprintf($fmt, $this->tbl, $emp, $from, $to, $content, $_SESSION['id']);
			// debug($sql);
			$db->query($sql);
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function template($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		$controller->assign_by_ref('template', $this->id(Constant::tbl_kq_template, $id));

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE status = '1'";
		$sql = sprintf($fmt, Constant::tbl_emp);
		// debug($sql);
		$result = $db->query($sql);

		$data = array();
		while ($row = $db->fetch_array($result)) {
			$data[] = $row;
		}

		$controller->assign_by_ref('result', $data);

		$controller->display($request, 'kq/plan/template');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function templatePost($controller, $request) {
		$this->pretreat($controller);

		$template = $request['id'];

		$list = $request['list'];
		if (empty($list)) {
			$controller->assign('baseLink', $this->doGet($request, 'findPost'));
			$this->findPost($controller, $request);
			return true;
		}

		$bb = explode(',', $list);
		$db = newdb($this->dsn_w);

		$fmt = "SELECT * FROM %s WHERE template = '%s' AND status = '1'";
		$sql = sprintf($fmt, Constant::tbl_kq_template_plan, $template);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			reset($bb);

			while (list($k, $emp) = each($bb)) {
				if (empty($emp) || $emp == '-') {
					continue;
				}

				$this->_template($template, $emp, $row);
			}
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	function _template($template, $emp, $row) {
		$db = newdb($this->dsn_w);
		$t1 = $row['t1'];
		$t2 = $row['t2'];

		$fmt = "SELECT * FROM %s WHERE emp = '%s' AND t1 = '%s' AND t2 = '%s'";
		$sql = sprintf($fmt, $this->tbl, $emp, $t1, $t2);
		$result = $db->query($sql);
		if ($db->num_rows($result) > 0) {
			$fmt = "UPDATE %s SET status = '2', modifytime = NOW() where emp = '%s' AND t1 = '%s' AND t2 = '%s'";
			$sql = sprintf($fmt, $this->tbl, $emp, $t1, $t2);
		} else {
			$fmt = "INSERT INTO %s(emp, t1, t2, oper, status, createtime, modifytime) VALUES('%s', '%s', '%s', '%s', '2', NOW(), NOW())";
			$sql = sprintf($fmt, $this->tbl, $emp, $t1, $t2, $_SESSION['id']);
		}
		// debug($sql);
		$db->query($sql);
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'kq/plan/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);

		$t1 = $request['t1'];
		$t2 = $request['t2'];

		$content = addslashes($request['content']);
		$status = $request['status'];

		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET office = '%s', dept = '%s', emp = '%s', t1 = '%s', t2 = '%s', content = '%s', status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $t1, $t2, $content, $status, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function copy($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'kq/plan/copy');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->assign_by_ref('record', $this->_plan_record($row));

		$controller->assign_by_ref('vacation', $this->_plan_vacation($row));

		$controller->display($request, 'kq/plan/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
	
		$row['week'] = $this->week($row['t1']);
		$row['week_t'] = $this->week($row['t1']);
		$row['state_t'] = $this->state[$row['state']];
	
		$row = $this->_view_t($row);
	
		return $row;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function cal($controller, $request) {
		$this->pretreat($controller);

		$controller->assign('t1', strtotime('-1 month'));
		$controller->assign('t2', strtotime('+1 day'));

		$controller->display($request, 'kq/plan/cal');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function calPost($controller, $request) {
		$this->pretreat($controller);

		$emp = $this->_input($request['emp']);

		$date1 = $request['date1'];
		$date2 = $request['date2'];

		$content = addslashes($request['content']);

		$db = newdb($this->dsn_r);
		// $fmt = "SELECT * FROM %s WHERE status = '2' AND t1 >= '%s' AND t2 < '%s'";
		$fmt = "SELECT * FROM %s WHERE t1 >= '%s' AND t2 < '%s'";
		$sql = sprintf($fmt, $this->tbl, $date1, $date2);
		if (!empty($emp)) {
			$sql .= " AND emp = '" . $emp . "'";
		}
		// debug($sql);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$this->_cal_status($row);
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	function _cal_status($data) {
		if (strtotime($data['t2']) > time()) {
			return $this->status[$data['status']];
		}

		if ($this->_vacation($data)) {
			$status = 9;
		} else {
			$status = $this->_record($data);
		}

		if ($status <> $data['status']) {
			$tt = $this->_tt($data['t1'], $data['t2']);

			$db = newdb($this->dsn_w);
			$fmt = "UPDATE %s SET tt = '%s', status = '%s' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $tt, $status, $data['id']);
			// debug($sql);
			$db->query($sql);
		}

		return $this->status[$status];
	}

	function _plan_record($data) {
		$dd = '%Y-%m-%d';

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE emp = '%s' AND DATE_FORMAT(time, '%s') = DATE_FORMAT('%s', '%s')";
		$sql = sprintf($fmt, Constant::tbl_kq_record, $data['emp'], $dd, $data['t1'], $dd);
		debug($sql);
		$result = $db->query($sql);
		$a = $db->fetch_all($result);

		if (date("Y-m-d", strtotime($data['t1'])) == date("Y-m-d", strtotime($data['t2']))) {
			return $a;
		}

		$fmt = "SELECT * FROM %s where emp = '%s' AND DATE_FORMAT(time, '%s') = DATE_FORMAT('%s', '%s')";
		$sql = sprintf($fmt, Constant::tbl_kq_record, $data['emp'], $dd, $data['t2'], $dd);
		debug($sql);
		$result = $db->query($sql);
		$b = $db->fetch_all($result);

		return array_merge($a, $b);
	}

	function _plan_vacation($data) {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE emp = '%s' AND t1 <= '%s' AND t2 >= '%s'";
		$sql = sprintf($fmt, Constant::tbl_kq_vacation, $data['emp'], $data['t1'], $data['t2']);
		// debug($sql);
		$result = $db->query($sql);
		$ret = array();
		while ($row = $db->fetch_array($result)) {
			$row['date'] = $this->_date($row['t1'], $row['t2']);

			$ret[] = $row;
		}

		return $ret;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function group($controller, $request) {
		$this->pretreat_group($controller);

		// $controller->assign('t1', strtotime('-1 month'));
		$controller->assign('t2', strtotime('+1 day'));
		
		$controller->assign('result_s', "300");

		$num = array(
			'1' => '按次数',
			'2' => '按工时',
		);
		$controller->assign('num_option', $num);

		$controller->display($request, 'kq/plan/group');
		return true;
	}

	function groupby($controller, $px) {
		$px = $this->groupby_aa($px, $this->groupby, 'emp');

		$controller->assign('groupby', $this->groupby[$px]);

		return $this->groupby_dd($px);
	}

	function groupby_dd($px) {
		if ($px == '1') {
			return "DATE_FORMAT(t1, '%Y-%m-%d %H')";
		}

		if ($px == '2') {
			return "DATE_FORMAT(t1, '%Y-%m-%d')";
		}

		if ($px == '3') {
			return "DATE_FORMAT(t1, '%Y-%m')";
		}

		return $px;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function groupPost($controller, $request) {
		$this->pretreat($controller);
			
		$this->groupPara($controller, $request);

		$num_s = $request['num_s'];
		if (!empty($num_s) && $num_s == '2') {
			$fmt = "SELECT %s AS a, SUM(tt) AS c FROM %s a ";
		} else {
			$fmt = "SELECT %s AS a, COUNT(tt) AS c FROM %s a ";
		}
		$sql = sprintf($fmt, $this->s, $this->tbl);

		$where = $this->_where($request);

		$request['sql'] = sprintf("%s %s GROUP BY %s ORDER BY %s %s", $sql, $where, $this->s, $this->orderby, $this->desc);

		$this->_group($controller, $request);

		// $controller->display($request, 'list');
		return true;
	}

	function _name($row, $px) {
		if (empty($px) || $px == '-' || $px == 'emp') {
			$row['a'] = sprintf("%s [%s]", Name::emp($row['a']), $row['a']);
		} else {

			$row = $this->_name_t($row, $px);
		}

		return $row;
	}

	function _num_t($t) {
		return $this->_time_t($t);
	}

	function _time_t($t) {
		$minute = 60;
		$hour = 60 * $minute;
		$day = 24 * $hour;

		return number_format($t / $hour, 2, '.', '');
	}

	function z($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn);
		$fmt = "SELECT * FROM %s";
		$sql = sprintf($fmt, $this->tbl);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$zjm = getPinyin($row['name']);
			$zjm = strtoupper($zjm);

			$fmt = "UPDATE %s SET zjm = '%s' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $zjm, $row['id']);
			// debug($sql);
			$db->query($sql);
		}

		logdate($controller, $request);
		return true;
	}

	/**
	 * 删除
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function d($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn);

		$fmt = "DELETE FROM %s WHERE ";
		$sql = sprintf($fmt, $this->tbl);

		$where = $this->_where2($request);
		if (!empty($where)) {
			$sql .= $where;
		} else {
			$controller->assign('baseLink', $this->doGet($request, 'findPost'));
			$this->findPost($controller, $request);
			return true;
		}

		// debug($sql);
		$db->query($sql);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	function _where2($request) {
		$emp_s = $this->_input($request['emp_s']);

		$date1_s = $request['date1_s'];
		$date2_s = $request['date2_s'];

		if (!empty($emp_s)) {
			$where .= " AND emp = '" . $emp_s . "'";
		}

		if (!empty($date1_s)) {
			$where .= " AND t1 >= '" . $date1_s . "'";
		}

		if (!empty($date2_s)) {
			$where .= " AND t2 <= '" . $date2_s . "'";
		}

		return ltrim($where, " AND");
	}

	function t($controller, $request) {
		$this->pretreat($controller);

		$file = "/home/sunxy/a.xls";
		$this->_xls($file);

		logdate($controller, $request);
		return true;
	}

	function _xls($file) {
		require_once APP_LIB . 'excel_class.php';

		Read_Excel_File($file, $return);
		// debug(print_r($return, true));

		// print_r($return);

		$ret = array();
		$buf = $return['Sheet2'];

		$db = newdb($this->dsn_w);
		for($j = 1; $j < count($buf); $j++) {
			$a = $buf[$j];

			print_r($a);
			$len = count($a);
			for ($i = 0; $i < $len; $i++) {
				if ($this->_is_time($a[$i])) {
					$k = $a[$i];
					$ret[$k] = $k;
				}
			}
		}

		print_r($ret);
	}

	function b($controller, $request) {
		$this->pretreat($controller);
		$s = "19:45-23:45";

		if ($this->_is_time($s)) {
			echo "OK";
		} else {
			echo "222";
		}

		logdate($controller, $request);
		return true;
	}

	function _is_time($s) {
		if (preg_match("/^(\d{2}):(\d{2})-(\d{2}):(\d{2})$/", $s)) {
			return true;
		}

		return false;
	}
}
?>